<?
/***************************************************************************
 *                              act_header.php
 *                           --------------------
 *   begin                : Friday, Aug 19, 2005
 *   copyright            : (C) 2005 cyber8 netWorks
 *   email                : support@cyber8.net 
 *
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/
 
		   
		   
// SERVER variables
$href     = $_SERVER["SCRIPT_NAME"];
$host     = $_SERVER["REMOTE_ADDR"];
$me       = $_SERVER["HTTP_HOST"];
$qs       = $_SERVER["QUERY_STRING"]; 
$source   = $_SERVER["HTTP_REFERER"];  
$agent    = $_SERVER["HTTP_USER_AGENT"]; 

// image processing
if (strlen($_GET ["i"]) > 0) LoadBin64Images($_GET ["i"]);
if (strlen($_GET ["z"]) > 0) LoadBin64Images($_GET ["z"],"u");

// CONFIG variables
$pathdata = file_to_array("path.config");
$path     = trim($pathdata[0]);
$dirname  = trim($pathdata[1]);
$metadata = array();

// GET variables
$d       = $_GET["d"];       /* directory */
$f       = $_GET["f"];       /* selected file index NOTE: THIS WAS A BAD IDEA! FIND SOMETHING ELSE*/
$r       = $_GET["r"];       /* image resize flag */
$h       = $_GET["h"];
$m       = $_GET["m"];
$ql      = $_GET["l"];       /* toolbar flag NOTE: DEPRECATE THIS: APP NOW HAS NO TOOLBARS */
$sc_l    = $_GET["s"];       /* scroll left */
$sc_t    = $_GET["t"];       /* scroll top */
$qw      = $_GET["w"];       /* workspace width */
$qm      = $_GET["qm"];      /* admin password */
$qp      = $_GET["qp"];      /* folder password */
$mz      = $_GET["mz"];      /* media player zoom factor */
$hm      = $_GET["hm"];      /* sidebar mode */
$mm      = $_GET["mm"];      /* media player mute flag */
$sw      = $_GET["sw"];      /* splitter width */
$cc      = $_GET["cc"];      /* continuous play flag */ 
$fo      = $_GET["fo"];      /* folders flag */ 
$rm_d    = $_GET["rd"];      /* remote folder */ 
$rm_f    = $_GET["rf"];      /* remote file */ 
$efile   = $_GET["efile"];   /* image editor file */ 
$mfile   = $_GET["mfile"];   /* text editor file */ 
$remove  = $_GET["remove"];  /* editconfig file remove */
$select  = $_GET["select"];  /* editconfig file select */
$newzoom = $_GET["zoom"];    /* image editor zoom factor */
$pda     = $_GET["pda"];     /* DEBUG DATA: supports pocket pc emulator */
$bkb     = $_GET["bkb"];     /* DEBUG DATA: supports blackberry emulator */
$sl      = $_GET["sl"];      /* file list toggle */
$session = $_GET["u"];
$edit    = $_GET["e"];       /* edit mode */
$view    = $_GET["v"];       /* view mode */
$master  = $_GET["j"];       /* admim mode flag */ 

// POST variables
$p_pm    = $_POST["p_pm"];
$p_ps    = $_POST["p_ps"];
$p_pv    = $_POST["p_pv"];
$p_pf    = $_POST["p_pf"];
$fu_f    = $_POST["fufile"];
$fm_f    = $_POST["ipmodefile"]; 
$fu_d    = $_POST["fufolder"];
$fm_d    = $_POST["ipmodefolder"];
$fn_o    = $_POST["oldfilename"];
$fn_n    = $_POST["newfilename"];

$ht_c    = $_POST["html"];
$ht_t    = $_POST["scrollT"];
$ht_l    = $_POST["scrollL"];
$ht_a    = $_POST["Iaddress"];

if (strlen($_GET["ht_t"]) > 0)
{
    $ht_t = $_GET["ht_t"];
}

if (strlen($_GET["ht_l"]) > 0)
{
    $ht_l = $_GET["ht_l"];
}

if (strlen($ht_t) > 0)
{
    $sc_t = $ht_t;
}

if (strlen($ht_l) > 0)
{
    $sc_l = $ht_l;
}


define ("ERROR_NOT_IMAGE", "The file you uploaded is not an image. Please upload a valid GIF, JPEG or PNG file.");

define ("CLIENT_JS_XML", "xmlextras.js");
define ("CLIENT_JS_MAIN", "scripts/_ps_main.js");
define ("CLIENT_JS_AUTOCOMPLETE", "http://www.101stratford.com/webservices/dxml/js/autocomplete.js");
 

if (strlen($rm_d)>0&&(is_dir($rm_d)||file_exists($rm_d))) { // if the file or folder exists, this can't
     $qs = str_replace("rd=".$rm_d,"",$qs);                 // be a remote folder...for now this is the
     $qs = str_replace("d=".$d,"d=".$rm_d,$qs);             // only way I can think of to control tree-
	 header ("Location: $href?$qs");                        // view navigation
	 exit;
}
 

if (strlen($ht_c)>0) // text file saving 
{ 
 
	// parse file service data 
	$char_pos_bin=strpos($ht_a,"?bin=");
	
	if ($char_pos_bin===false)  // local file save
	{	
		WriteToFile($ht_a, stripslashes($ht_c)); 
		$sc_l = $ht_l;
		$sc_t = $ht_t; 		 
	} 
	else  // remote file save
	{
	
		// path before querystring
	    $service_address   = substr($ht_a, 0, $char_pos_bin);
		
		// path after querystring
		$service_fullpath  = substr($ht_a, $char_pos_bin+5); 
		
		// parse filename from path 
		$service_filename  = filetitle ($service_fullpath);
		$service_filepath  = str_replace($service_filename, "", $service_fullpath);
		
		// encoded file data
		$service_filedata  = base64_encode ( stripslashes( trim($ht_c) ) );
		
		// parse domain and service script name
		$address_sans_http = substr($service_address, 7);
		$char_pos_slash    = strpos($address_sans_http, "/"); 
		$service_domain    = substr($address_sans_http, 0, $char_pos_slash); 
		$service_post_path = substr($address_sans_http, $char_pos_slash);  
		
		// create file service request 
		$service_request   =  "<request method='upload' file=\"$service_fullpath\">\n";
		$service_request  .= "    <file xmlns:dt='urn:schemas-microsoft-com:datatypes' 
		                                   dt:dt='bin.base64' target=\"".$service_filepath."\" 
										   name=\"".$service_filename."\">".$service_filedata; 
		$service_request  .= "    </file>\n";
		$service_request  .= "</request>"; 
		
		// send save request
		$service_result    = sendToPHPDc ($service_domain, "POST", $service_post_path, $service_request, 0);
		
		
		print "<xmp>$service_request</xmp>";
		print "<xmp>$service_result</xmp>";
	
	}
	
	$char_pos_ht_t = strpos($qs, "ht_t");
	$char_pos_ht_l = strpos($qs, "ht_l");
	if ( $char_pos_ht_t===false )
	{
	    $qs .= "&ht_t=" . $ht_t;
	}
	else
	{
	    $qs = str_replace("ht_t", "4", $qs );
	    $qs .= "&ht_t=" . $ht_t;
	}
	
	if ( $char_pos_ht_l===false )
	{
	    $qs .= "&ht_l=" . $ht_l;
	}
	else
	{
	    $qs = str_replace("ht_l", "4", $qs );
	    $qs .= "&ht_l=" . $ht_t;
	}
	
	print "<a href='$href?$qs'>Back to editor</a>";
	response_redirect ($href."?".$qs) ;
	exit;
 
} // file saving



// BEGIN ADMIN functions -----------------------------------' 
if (strlen($fn_n)>0) // file rename 
{
   $fn_t = filetitle ($fn_o);
   $fn_t = str_replace($fn_t,$fn_n,$fn_o);
   rename ($fn_o,$fn_t);
   header ("Location: $href?$qs");
   exit;
}
 
if (strlen($fu_f)>0) // file permissions update  
{
   chmod($fu_f,$fm_f);
   header ("Location: $href?$qs");
   exit;
}
 
if (strlen($fu_d)>0) // folder permissions update  
{
   chmod($fu_d,$fm_d);
   header ("Location: $href?$qs");
   exit;
}
// END ADMIN functions -------------------------------------' 
 

// current directory name
if (strlen($d)>0)  $dirname = $d; 

// panel width for preview window
if (strlen($qw)>0) {
   $preview_width = floor($qw/132);
} else $preview_width = 6; 

// resize data for sizable media players
if ($mz=="") $mz=1;
$mw=$mz*320;
$mh=$mz*240;


// stuff
$parent        = "";                                            /* deprecated */
$folders       = Array();                                       /* deprecated */
$files         = Array();                                       /* deprecated */
$is_media      = !(strpos(strtolower($dirname),"@",0)===false); /* deprecated */
$is_service    = !(strpos(strtolower($dirname),"`",0)===false); /* deprecated */
$is_virtual    = !(strpos(strtolower($dirname),"*",0)===false); /* deprecated */
$is_rss        = !(strpos(strtolower($dirname),"^",0)===false); /* deprecated */
$is_thumbnails = $view=="t";
$is_details    = $view=="d";
$is_admin      = false; 
$is_pocketpc   = (!(strpos(strtolower($agent),"ppc",0)===false))||(strlen($pda)>0); 
$is_blackberry = (!(strpos(strtolower($agent),"blackberry",0)===false))||(strlen($bkb)>0);
$showlist      = strlen($sl)>0;

 
if ($is_blackberry) 
{
    $is_pocketpc=true;
	$bkb  = "bkb"; /* DEBUG DATA: supports blackberry emulator */
    $view = "d"; 
    $edit = "b"; 
} 
else if ($is_pocketpc) 
{
    $is_blackberry=false;
}
else if ($ql=="") 
{
    header ("Location: launch.php");
}
 
 
if ($is_pocketpc) 
{ 
	$view = "d"; 
	$bkb = "";
}
 
if (count($_FILES)>0)  // read uploaded files and add them to the user's config file
{
	foreach( $_FILES as $file_name => $file_array ) 
	{ 
	
		$upload_path = $file_array['tmp_name'];
		$upload_name = $file_array['name'];
		$upload_type = $file_array['type'];
		$upload_size = $file_array['size']; 
		
		
		if (isreadable ($upload_path)) 
		{
			if ( is_uploaded_file( $upload_path )  ) 
			{
				$tmp = $dirname."/".$upload_name;
				$redirect_success = script_block ("location.href='".$href."?e=e&r=on&efile=".(str_replace($path,"",$tmp))."'"); 
				copy($upload_path, $tmp) or die("unable to create info");	 
				print $redirect_success;
			}  
		} 
		else 
		{
		    print ERROR_NOT_IMAGE;
		}
		exit;  
	} 
} // end upload file handler


 
// BUILD FILE/FOLDER LIST
$this_folder  = new pimp_folder ($dirname, $rm_d);

// check folder for errors before continuing
if ( $this_folder->pimp_error_code>0) 
{
   print $pimp_error_codes[$this_folder->pimp_error_code];
   exit;
}

$page_folders = $this_folder->pimp_folders; 
/* 
   BUILD FILE/FOLDER LIST
 -----------------------------------------------------------'
*/ 


// SELECTED FILE DETAILS  
$e=0;
while (list($file_info_key, $file_object) = each($this_folder->pimp_files))   
{  
  if ($e==$f) 
  {
      $page_file = $file_object;
	  break;
  }
  $e++; 
}
$filename = $page_file["path"]; 
if (strlen($mfile)>0) 
{
    $filename = $mfile;
} 
else if (strlen($efile)>0) 
{
    $filename = $efile;
} 

$scroll = 0; 
if (strlen($sc_l)>0) 
{
    $scroll = $sc_l;
}
$ft = $page_file["type"];
$fs = $page_file["size"];
$fd = $page_file["date"];
/*
   SELECTED FILE DETAILS
 -----------------------------------------------------------'
*/



// states and messages
if ($r=="") 
{
    $r = "off";
}
$off     = $r=="on"?"off":"on";
$message = $r=="on"?"Show images full size":"Size images to window";
$panelon = $edit=="e"?"display:none":"";
/*
   states and messages
 -----------------------------------------------------------'
*/


// PROCESS EDITOR METAFILE 
$editcookie = $_COOKIE["editcookie"]; 
$editconfig = "_PIMP_edit_data_".$editcookie.".log";
 
if (($edit=="e")||($edit=="t"))   // file management
{
	if (strlen($editcookie)>0) 
	{ 
	 
		$file_roster     = file_to_array($editconfig); 
		$file_contents   = "";
		$default_file    = "";
		$file_is_unique  = true;  
		$path_to_compare = str_replace("=","~",$filename);
		
		// rebuld file
		for ($e=1;$e<count($file_roster);$e++) 
		{
		     parse_str($file_roster[$e]); 
			 if ($file==$path_to_compare)  
			 {
			     $file_is_unique=false;
				 if (strlen($newzoom)>0) 
				 {
				     $file_roster[$e]="file=".$file."&name=".$name."&size=".$size."&zoom=".$newzoom;
				 }  
			 }
			 if ($text==$path_to_compare) 
			 {
			     $file_is_unique=false;   
			 }
			 
			 if ( strlen(trim($remove))>0&&stripslashes(trim($remove))==stripslashes(trim($file)) ) 
			 {
			     // if a removal is indicated, don't write this line
			 } 
			 else if ( strlen(slashout($remove))>0&&slashout($remove)==slashout(str_replace("=","~",$text)) ) 
			 {
			     // if a removal is indicated, don't write this line 
			     if ($remove!=$mfile) 
				 {
				     $default_file = str_replace("=","~",$mfile);
				 }
			 } 
			 else
			 { 
			     $file_contents.=$file_roster[$e]."\n";  
				 if ($default_file=="") 
				 {  
				     $default_file = str_replace("=","~",$edit=="t"?$text:$file);
				 }
			 }
		}  
		
		 // desginate selected file 
		if (strlen($remove)>0) 
		{ // revert selected file to default file if a file is removed
		    $file_contents = "selected=".$default_file."\n".$file_contents;
		} 
		else if (strlen($select)>0) 
		{ // change selected file
		    $file_contents = "selected=".$select."\n".$file_contents;
		} 
		else if ($remove=="") 
		{ // set selected file to opened file
		    $file_contents = "selected=".$path_to_compare."\n".$file_contents;
		} 
		else 
		{ // reset selected file to currently selected file
		    parse_str($file_roster[0]);
		    $file_contents = "selected=".$selected."\n".$file_contents;
		}  
		
		// add new file to list
		if (($remove=="")&&$file_is_unique) 
		{ 
			if (my_content_type($path_to_compare)=="txt") 
			{ // add text file to editor list
				$file_contents.="text=".$path_to_compare."&name=".filetitle($filename)."\n";
			} 
			else 
			{ // add image file to editor list
				$oi = @imagecreatefromjpeg($path_to_compare); 
				list($lw,$lh) = getimagesize($path_to_compare);   
				$file_contents.="file=".$path_to_compare."&name=".filetitle($filename)."&size=".$lw.",".$lh."&zoom=1\n";
			}
		}   
		 
		// rewrite file
	    WriteToFile($editconfig, $file_contents);
		
		// write result message
		if (strlen($remove)>0) 
		{
		    $qs = str_replace("mfile=$mfile","mfile=$default_file",$qs);
		    $qs = str_replace("remove=$remove","",$qs);
		    if ($default_file=="") $qs = str_replace("e=t","e=b",$qs); 
		    $qs = slashout($qs);
			print "<xmp>$file_contents</xmp>";
		    print $remove. " <a href='$href?$qs'>closed</a>!<br>".$qs;
			response_redirect ($href."?".$qs) ;
			exit;
		}
		if (strlen($select)>0) 
		{
		    print $select. " selected!";
			exit;
		} 
	} 
	else 
	{ 
	
// where no file exists, create one and save its name as a cookie
	    savecookie("editcookie", $host, $qs);
	} 
}
/*
   END PROCESS EDITOR METAFILE
 -----------------------------------------------------------'
*/


// window height offsets
$window_height_offset=$edit=="e"?"50":"170"; // EDIT/FILMSTRIP VIEWS
$window_height_offset=$view=="t"?10:$window_height_offset;     // THUMBNAIL VIEW 
$window_height_offset=$view=="d"?10:$window_height_offset;     // DETAIL VIEW 
$window_height_offset=$is_admin?10:$window_height_offset;      // ADMIN VIEW 
if ($view==""&&$pv=="on") $window_height_offset-=120; // filmstrip preview off

?>